// 自定义异常，返回的信息
import { HttpException, HttpStatus } from '@nestjs/common';
import { BadRequestException } from '@nestjs/common';
import { dtoErrorType } from '../services/validation.service';
import {
  userErrorMessages,
  errorMessages,
} from '../services/validation.service';
import { ErrorInfo } from '../services/validation.service';

// 自定义的异常，触发异常 api 直接返回异常信息。
interface Iconstructor extends ErrorInfo {
  data?: any;
}
export class ErrorResponseException extends HttpException {
  constructor(response: Iconstructor, httpResponseStatus?: number) {
    const httpStatus = httpResponseStatus || HttpStatus.OK;
    super(response, httpStatus, {
      cause: new Error('custom exception'),
    });
  }
}

// dto 验证失败的,以及使用到统一的错误信息的
export class ValidationException extends BadRequestException {
  constructor(
    public dtoType: dtoErrorType,
    validationErrors?: any,
  ) {
    super({
      errno: errorMessages[dtoType].errno,
      message: errorMessages[dtoType].message,
      data: validationErrors,
    });
  }
}

// jwt 校验失败
export class ValidationTokenException extends BadRequestException {
  constructor(validationErrors?: any) {
    super({
      errno: userErrorMessages.jwtError.errno,
      message: userErrorMessages.jwtError.message,
      data: validationErrors,
    });
  }
}
